package com.example.mactchoperator;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

class BluetoothMockServeur implements Runnable{
        
    public static void main(String argv[]) throws Exception {
//        @SuppressWarnings("resource")
//              ServerSocket welcomeSocket = new ServerSocket(6787);
//        while (true) {
//              Socket connectionSocket = welcomeSocket.accept();
//              BluetoothMockServeur serveur = new BluetoothMockServeur(connectionSocket);
//              Thread t = new Thread(serveur);
//              t.start();
//        }
         BluetoothMockServeur serveurtBT = new BluetoothMockServeur(6787); //

    }
    
    private Socket connectionSocket;
    private ServerSocket welcomeSocket;
    
    public BluetoothMockServeur(Socket client){
        connectionSocket = client;
    }
    public BluetoothMockServeur(int port){

                try {
                        welcomeSocket = new ServerSocket(port);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        Thread t = new Thread(this);
        t.start();
    }

        @Override
        public void run() {
                while (true) {
                        System.out.println("coucou3");
                        while(connectionSocket == null || !connectionSocket.isConnected()) {
                                try {
                                        System.out.println("coucou");
                                        connectionSocket = welcomeSocket.accept();
                                        System.out.println("coucou2");
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }                       
                        }
                        while(connectionSocket != null && connectionSocket.isConnected()){
                    try {
                        BufferedReader inFromClient
                                = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                        DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                        String clientSentence = inFromClient.readLine();
                        if(clientSentence == null){
                                return;
                        }
                        
                        if(clientSentence.startsWith("-1")) {
                                SyncData.setMainNotConnected(true, Integer.parseInt(clientSentence.split(":")[1]));                     
                        }else if(clientSentence.startsWith("-2")) {
                                SyncData.setMainNotConnected(false, Integer.parseInt(clientSentence.split(":")[1]));                    
                        }else if(clientSentence.startsWith("-3")) {
                            SyncData.TCPDataSentViaSync(Integer.parseInt(clientSentence.split(":")[1]));                    
                        }else {
                                System.out.println("Received: " + SyncData.TableData.moves[Integer.parseInt(clientSentence) - 1]);
                                SyncData.SyncMove(Byte.parseByte(clientSentence));                      
                        }System.out.println("Received: " + clientSentence);
                        
                        
                        outToClient.writeBytes(clientSentence + "\n");
                    } catch (Exception e) {
                        connectionSocket = null;
                        e.printStackTrace();
                    }
                }
                }
        }
}